﻿using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class OrderMap : EntityTypeConfiguration<Order> {
        public OrderMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.pfm_orders");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(a => a.OrderId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // None

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // Symbol
            HasRequired<Symbol>(a => a.Symbol)
                .WithMany()
                .HasForeignKey(a => a.SymbolId);

            // Managed Product
            HasRequired<ManagedProduct>(a => a.ManagedProduct)
                .WithMany()
                .HasForeignKey(a => a.ManagedProductId)
                .WillCascadeOnDelete(false);

            // OrderConstraint
            HasRequired<OrderConstraint>(a => a.OrderConstraint)
                .WithMany()
                .HasForeignKey(a => a.OrderConstraintId);

            // OrderExecution
            HasMany(a => a.OrderExecutions)
                .WithRequired(a => a.Order)
                .HasForeignKey(a => a.OrderId);


            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(a => a.OrderId)
                .IsRequired()
                .HasColumnName("mpo_id");

            Property(a => a.ManagedProductId)
                .IsRequired()
                .HasColumnName("mpo_mpd_id");

            Property(a => a.SymbolId)
                .IsRequired()
                .HasColumnName("mpo_sym_id");

            Property(a => a.Quantity)
                .IsRequired()
                .HasColumnName("mpo_quantity");

            Property(a => a.MaximalAmount)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("mpo_max_amount");

            Property(a => a.EntryDate)
                .IsRequired()
                .HasColumnName("mpo_entry_dt");

            Property(a => a.ValidFromDate)
                .IsRequired()
                .HasColumnName("mpo_valid_frm_dt");

            Property(a => a.ValidUntilDate)
                .IsOptional()
                .HasColumnName("mpo_valid_til_dt");

            Property(a => a.OrderConstraintId)
                .IsRequired()
                .HasColumnName("mpo_moc_id");

            Property(a => a.Limit)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("mpo_limit");

            Property(a => a.Cancelled)
                .IsRequired()
                .HasColumnName("mpo_cancelled_fg");

            Property(a => a.Executed)
                .IsRequired()
                .HasColumnName("mpo_executed_fg");
        }
    }
}
